<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CFacebook
 *
 * @author andrea
 */
class CFacebook_cuspo extends Controller_cuspo {

  public function getFormInsertFacebook() {
    /* @var $view View_cuspo */
    $view = USingleton_cuspo::getInstances("View_cuspo");
    $settings = array(
        "media_buttons" => false,
        "textarea_name" => "facebook_post",
        "textarea_rows" => 7
    );
    ob_start();
    wp_editor("Inserisci il post", "facebook_post", $settings);
    $editor = ob_get_contents();
    ob_end_clean();
    $view->assignVar("name", "facebook");
    $view->assignVar("editor", $editor);
    return $view->fetchTpl("insert_facebook");
  }

  public function addFacebook() {
    //INIZIALIZZAZIONE VARIABILI
    global $debug, $config_cuspo;
    /* @var $found Foundation_cuspo */
    $found = USingleton_cuspo::getInstances("FFacebook_cuspo");
    /* @var $view View_cuspo */
    $view = USingleton_cuspo::getInstances("View_cuspo");
    /* @var $entity EConcorsi_cuspo */
    $entity = USingleton_cuspo::getInstances("EFacebook_cuspo");

//PRELIEVO INFORMAZIONI E CHECK
    $nome_utente = $view->getRequest("nome_utente");
    $url_profilo = $view->getRequest("url_profilo");
    $post = $view->getRequest("facebook_post");

//INSERIMENTO NEL DATABASE
    $fields = array(
        "nome_utente" => $nome_utente,
        "url_profilo" => $url_profilo,
        "testo" => $post,
    );
    $object = $entity->generateObject($fields);
    $query = $found->insertQuery($object);
    $found->executeQuery($query);
    return;
  }

  public function updateFacebook() {
//INIZIALIZZAZIONE VARIABILI
    global $debug, $config_cuspo;
    /* @var $found Foundation_cuspo */
    $found = USingleton_cuspo::getInstances("Foundation_cuspo");
    /* @var $view View_cuspo */
    $view = USingleton_cuspo::getInstances("View_cuspo");

//PRELIEVO INFORMAZIONI E CHECK
    $nome_utente = $view->getRequest("nome_utente");
    $url_profilo = $view->getRequest("url_profilo");
    $post = $view->getRequest("facebook_post");
    $id = $view->getRequest("id");
    $fields = array(
        array("nome_utente", $nome_utente),
        array("url_profilo", $url_profilo),
        array("testo", $post)
    );
//INSERIMENTO NEL DATABASE
    $query = $found->updateQuery($config_cuspo['tables']["facebook"], $fields, array(array("id", "=", $id)));
    $found->executeQuery($query);
    return;
  }

  public function getEditFacebookForm($post_id = 0) {
    if ($post_id == 0)
      return $this->getEditFacebookList();
    global $config_cuspo;
    /* @var $found Foundation_cuspo */
    $found = USingleton_cuspo::getInstances("Foundation_cuspo");
    /* @var $view View_cuspo */
    $view = USingleton_cuspo::getInstances("View_cuspo");
    $list = $this->getEditFacebookList();
    $query = $found->searchQuery(array("*"), $config_cuspo['tables']["facebook"], array(array("id", "=", $post_id)));
    $results = $found->getRowsAssoc($query);
    //Assegnazione delle variabili
    $settings = array(
        "media_buttons" => false,
        "textarea_name" => "facebook_post",
        "textarea_rows" => 7
    );
    ob_start();
    wp_editor($results[0]['testo'], "facebook_post", $settings);
    $editor = ob_get_contents();
    ob_end_clean();
    $view->assignVar("id", $post_id);
    $view->assignVar("posts_list", $list);
    $view->assignVar("editor", $editor);
    $view->assignVar("post_da_modificare", $results[0]);
    return $view->fetchTpl("edit_facebook_form");
  }

  public function getEditFacebookList() {
    global $config_cuspo, $debug;
    /* @var $view View_cuspo */
    $view = USingleton_cuspo::getInstances("View_cuspo");
    /* @var $found Foundation_cuspo */
    $found = USingleton_cuspo::getInstances("Foundation_cuspo");
    $query = $found->searchQuery(array("*"), $config_cuspo['tables']["facebook"])." ORDER BY id DESC";
    $results = $found->getRowsAssoc($query);
    $view->assignVar("risultati", $results);
    $view->assignVar("name", "faebook");
    $view->assignVar("operation", $config_cuspo['editPostsOperations']["facebook"]);
    return $view->fetchTpl("edit_facebook");
  }

  public function showFacebook($nr_posts) {
    global $config_cuspo, $debug;
    /* @var $view View_cuspo */
    $view = USingleton_cuspo::getInstances("View_cuspo");
    /* @var $found Foundation_cuspo */
    $found = USingleton_cuspo::getInstances("Foundation_cuspo");
    $query = $found->searchQuery(array("*"), $config_cuspo['tables']['facebook'], array(array("1","=","1")), "0,2"," id DESC");
    $results = $found->getRowsAssoc($query);
    $view->assignVar("risultati",$results);
    return $view->fetchTpl("show_facebook");
  }

}

?>
